//
//  LGPatientMedicineController.m
//  BreathTrainingTool
//
//  Created by 1234 on 2024/7/15.
//  患者档案-健康记录

#import "LGPatientMedicineController.h"
#import "LGPatientDetailMedicCell.h"
#import "LGMedicineUntowardEffectCell.h"
#import "LGPickerView.h"
@interface LGPatientMedicineController ()<UITableViewDelegate,UITableViewDataSource,LGPatientDetailMedicDelegate>
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)UILabel *timeLabel;
/**   */
@property (nonatomic , strong)UIButton *monthBtn;
@property (nonatomic , strong)NSDateFormatter *dayformatter;
@property (nonatomic , strong)NSDateFormatter *formatter;
@property (nonatomic , strong)NSDictionary *medicinDic;
@property (nonatomic , assign)NSInteger medicinIndex;
/**   */
@property (nonatomic , copy)NSString *currentMonth;
/**   */
@property (nonatomic , strong)NSArray *dataArray;
/**   */
@property (nonatomic , strong)NSArray *yearArray;
/**   */
@property (nonatomic , strong)NSArray *monthArray;

@end

@implementation LGPatientMedicineController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"健康记录";
    self.currentMonth = [self.formatter stringFromDate:[NSDate date]];
    self.yearArray = @[@"2008",@"2009",@"2010",@"2011",@"2012",@"203",@"2014",@"2015",@"2016",@"2017",@"2018",@"2019",@"2020",@"2021",@"2022",@"2023",@"2024",@"2025",@"2026",@"2027",@"2028",@"2029",@"2030"];
    self.monthArray = @[@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12"];
    [self.view addSubview:self.tableView];
    [self requestData];
}

-(void)setMedicineArray:(NSArray *)medicineArray{
    _medicineArray = medicineArray;
    if (medicineArray.count>0) {
        self.medicinIndex = 0;
        NSString *schemaid = LGNSString(medicineArray[0][@"id"]);
        [self requestMedicineChat:schemaid index:0];
    }
}

//请求服药统计图
-(void)requestMedicineChat:(NSString *)schemaid index:(NSInteger)index{
    self.medicinIndex = index;
    NSString *endDate = [self.dayformatter stringFromDate:[NSDate date]];
    NSDate *startDate = [[NSDate date] initWithTimeIntervalSinceNow:-24*60*60*30];
    NSString *startDay = [self.dayformatter stringFromDate:startDate];
    [RequestUtil withGET:@"/medicineTask/chart" parameters:@{@"schemaId":schemaid,@"patientId":self.patientid,@"startDay":startDay,@"endDay":endDate} header:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            self.medicinDic = returnData;
            [self.tableView reloadData];
        }
        
    } error:^(NSError *error) {
        
    }];
}

-(void)requestData{
    [RequestUtil withGET:@"/badness/statistics" parameters:@{@"yearMonth":self.currentMonth,@"patientId":self.patientid} header:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            self.dataArray = returnData;
            [self.tableView reloadData];
        }
    } error:^(NSError *error) {
        
    }];
}


//不良反应
-(void)setCurrentMonth:(NSString *)currentMonth{
    _currentMonth = currentMonth;
    [self.monthBtn setTitle:currentMonth forState:UIControlStateNormal];
    [self.monthBtn setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:5];
}

//不良反应-选择月份
-(void)selectMonthAction:(UIButton *)sender{
    LGPickerView *pickerView = [[LGPickerView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
    pickerView.pickArry = @[self.yearArray,self.monthArray];
    pickerView.units = @[@"年",@"月"];
    pickerView.selectStr = [self.currentMonth stringByReplacingOccurrencesOfString:@"-" withString:@"|"];
    pickerView.titleLabel.text = @"选择月份";
    [[ObjectTool mainWindow] addSubview:pickerView];
    __weak typeof(self) weakSelf = self;
    pickerView.selectPickViewResult = ^(NSArray *resultArray) {
        weakSelf.currentMonth = [NSString stringWithFormat:@"%@-%@",resultArray[0],resultArray[1]];
        [weakSelf requestData];
    };
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        //服药统计图
        return 1;
        
    }else{
        //不良反应
        return self.dataArray.count;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        //服药统计图
        LGPatientDetailMedicCell *medicinCell = [tableView dequeueReusableCellWithIdentifier:@"medicinCell" forIndexPath:indexPath];
        medicinCell.selectionStyle = UITableViewCellSelectionStyleNone;
        medicinCell.currentIndex = self.medicinIndex;
        medicinCell.medicineArray = self.medicineArray;
        medicinCell.dataDic = self.medicinDic;
        medicinCell.delegate = self;
        return medicinCell;
        
    }else if (indexPath.section == 1){
        //不良反应
        LGMedicineUntowardEffectCell *medicinCell = [tableView dequeueReusableCellWithIdentifier:@"effectCell" forIndexPath:indexPath];
        medicinCell.selectionStyle = UITableViewCellSelectionStyleNone;
        medicinCell.dataDic = self.dataArray[indexPath.row];
        return medicinCell;
    }
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return viewPix(52);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    LGCornerHeaderView *headView = [[LGCornerHeaderView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(52))];
    headView.topMargin = viewPix(8);
    NSString *title = (section == 0)?@"服药情况":@"不良反应";
    UILabel *titleLabel = [UILabel lableWithText:title colorString:@"#2B2A37" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    titleLabel.frame = CGRectMake(viewPix(32), viewPix(20), Screen_W-viewPix(150), viewPix(22));
    [headView addSubview:titleLabel];
    if (section == 0) {
        //服药情况
        [headView addSubview:self.timeLabel];
        [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(headView).offset(-viewPix(32));
            make.centerY.equalTo(titleLabel);
        }];
    }else if (section == 1){
        //不良反应
        [headView addSubview:self.monthBtn];
    }
    return headView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    CGFloat height = (section == 0)?0.01:viewPix(15);
    if (section == 1 && self.dataArray.count == 0) {
        height = viewPix(200);
    }
    return  height;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    CGFloat height = (section == 0)?0.01:viewPix(15);
    LGCornerFooterView *footView = [[LGCornerFooterView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, height)];
    footView.bottomMargin = viewPix(7);
    if (section == 1 && self.dataArray.count == 0) {
        footView.frame = CGRectMake(0, 0, Screen_W, viewPix(200));
        [self emptyViewWithSuperView:footView frame:CGRectMake(viewPix(16), 0, Screen_W-viewPix(32), viewPix(170))];
        self.isfirst = NO;
        self.emptyView.userInteractionEnabled = NO;
        self.emptyView.backgroundColor = [UIColor whiteColor];
        [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无数据" offset:viewPix(30)];
    }else{
        [self.emptyView stopBufferAction];
    }
    return footView;
}


-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGPatientDetailMedicCell class] forCellReuseIdentifier:@"medicinCell"];
        [_tableView registerClass:[LGMedicineUntowardEffectCell class] forCellReuseIdentifier:@"effectCell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.backgroundColor = [UIColor colorWithString:@"#F3F1FC"];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(8))];
    }
    return _tableView;
}
     

-(UILabel *)timeLabel{
    if(!_timeLabel){
        _timeLabel = [UILabel lableWithText:@"" colorString:@"#968F8F" textFont:LGFont(12) textAlignment:NSTextAlignmentRight lines:1];
    }
    return _timeLabel;
}

-(UIButton *)monthBtn{
    if(!_monthBtn){
        _monthBtn = [UIButton buttonWithTitle:@"" titleFont:LGFont(14) textColor:@"#2B2A37" imageName:@"grayTipDown"];
        _monthBtn.frame = CGRectMake(Screen_W-viewPix(140), viewPix(10), viewPix(110), viewPix(42));
        _monthBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [_monthBtn setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:5];
        [_monthBtn addTarget:self action:@selector(selectMonthAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _monthBtn;
}

-(NSDateFormatter *)formatter{
    if(!_formatter){
        _formatter = [[NSDateFormatter alloc]init];
        _formatter.dateFormat = @"yyyy-MM";
    }
    return _formatter;
}

-(NSDateFormatter *)dayformatter{
    if(!_dayformatter){
        _dayformatter = [[NSDateFormatter alloc]init];
        _dayformatter.dateFormat = @"yyyy-MM-dd";
    }
    return _dayformatter;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
